
#include "sprite.h"


LuaSprite::LuaSprite(UINT texture, float x, float y, float width, float height){
	m_spr = gge::Sprite_Create(texture?(gge::ggeTexture *)texture:0, x, y, width, height);
}
LuaSprite::~LuaSprite(){
	//gge::System_Log("SpriteRelease:%d",p);
	//if (PrintRelease)
	//	printf("SpriteRelease:%d\n",p); 
	GGE_RELEASE(m_spr);
}
void LuaSprite::Render() { 
	m_spr->RenderPosition();
}
void LuaSprite::SetPosition(float x, float y) {
	m_spr->SetPosition(x,y);
}
void LuaSprite::SetPositionEx(float x, float y, float rotation, float hscale, float vscale) {
	m_spr->SetPositionEx(x,y,rotation,hscale,vscale);
}
void LuaSprite::SetPositionStretch(float x1, float y1, float x2, float y2) {
	m_spr->SetPositionStretch(x1,y1,x2,y2);
}
void LuaSprite::SetPosition4V(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) {
	m_spr->SetPosition4V(x0,y0,x1,y1,x2,y2,x3,y3);
}
void LuaSprite::SetTexture(UINT texture) {
	m_spr->SetTexture(texture?(gge::ggeTexture *)texture:0);
}
void LuaSprite::SetTextureRect(float x, float y, float width, float height) {
	m_spr->SetTextureRect(x,y,width,height);
}
void LuaSprite::SetTextureFilter(bool b) {
//	m_spr->SetTextureFilter(b);
}
void LuaSprite::SetColor(gge::gUInt color, int i) {
	m_spr->SetColor(color,i);
}
void LuaSprite::SetZ(float z, int i) {
	m_spr->SetZ(z,i);
}
void LuaSprite::SetBlendMode(int blend) {
	m_spr->SetBlendMode(blend);
}
void LuaSprite::SetHotSpot(float x, float y) {
	m_spr->SetHotSpot(x,y);
}
void LuaSprite::SetFlip(bool bX, bool bY, bool bHotSpot) {
	m_spr->SetFlip(bX,bY,bHotSpot);
}
bool LuaSprite::IsTextureFilter() {
	return false;
//	return m_spr->IsTextureFilter();
}
gge::gUInt LuaSprite::GetColor(int i) {
	return m_spr->GetColor(i);
}
float LuaSprite::GetZ(int i) {
	return m_spr->GetZ(i);
}
int LuaSprite::GetBlendMode() {
	return m_spr->GetBlendMode();
}
float LuaSprite::GetWidth() {
	return m_spr->GetWidth();
}
float LuaSprite::GetHeight() {
	return m_spr->GetHeight();
}

LuaTable LuaSprite::GetHotSpot(){ 
	float bX,bY;
	m_spr->GetHotSpot(&bX,&bY);
	LuaTable t(L);
	t.set(1,bX);
	t.set(2,bY);
	return t;
}
LuaTable LuaSprite::GetFlip(){ 
	bool bX,bY;
	m_spr->GetFlip(&bX,&bY);
	LuaTable t(L);
	t.set(1,bX);
	t.set(2,bY);
	return t;
}
LuaTable LuaSprite::GetTextureRect(){ 
	float bX,bY,bW,bH;
	m_spr->GetTextureRect(&bX,&bY,&bW,&bH);
	LuaTable t(L);
	t.set(1,bX);
	t.set(2,bY);
	t.set(3,bW);
	t.set(4,bH);
	return t;
}
void LuaSprite::Copy(UINT spr){
	m_spr->Copy((gge::ggeSprite*)spr);
}
UINT LuaSprite::Clone(){
	return (UINT)m_spr->Clone();
}
UINT LuaSprite::GetTexture(){
	return (UINT)m_spr->GetTexture();
}
void LuaSprite::SetP(UINT v){
	m_spr = (gge::ggeSprite*)v;
}
UINT LuaSprite::GetP(){ 
	return (UINT)m_spr;
}
int LuaSprite::GetRefCount(){ 
	return m_spr->GetRefCount();
}
void LuaSprite::AddRef(){ 
	m_spr->AddRef();
}
void LuaSprite::Release(){ 
	GGE_RELEASE(m_spr);
}
int REG_GGE_Sprite(lua_State *L){
	const char* gname = "__ggeSprite";
	ELuna::registerClass<LuaSprite>(L, gname, ELuna::constructor<LuaSprite, UINT,float, float, float, float >);

	ELuna::registerMethod<LuaSprite>(L, "Copy", &LuaSprite::Copy);
	ELuna::registerMethod<LuaSprite>(L, "Clone", &LuaSprite::Clone);
	ELuna::registerMethod<LuaSprite>(L, "Render", &LuaSprite::Render);
	ELuna::registerMethod<LuaSprite>(L, "SetPosition", &LuaSprite::SetPosition);
	ELuna::registerMethod<LuaSprite>(L, "SetPositionEx", &LuaSprite::SetPositionEx);
	ELuna::registerMethod<LuaSprite>(L, "SetPositionStretch", &LuaSprite::SetPositionStretch);
	ELuna::registerMethod<LuaSprite>(L, "SetPosition4V", &LuaSprite::SetPosition4V);
	ELuna::registerMethod<LuaSprite>(L, "SetTexture", &LuaSprite::SetTexture);
	ELuna::registerMethod<LuaSprite>(L, "SetTextureRect", &LuaSprite::SetTextureRect);
	ELuna::registerMethod<LuaSprite>(L, "SetTextureFilter", &LuaSprite::SetTextureFilter);
	ELuna::registerMethod<LuaSprite>(L, "SetZ", &LuaSprite::SetZ);
	ELuna::registerMethod<LuaSprite>(L, "SetBlendMode", &LuaSprite::SetBlendMode);
	ELuna::registerMethod<LuaSprite>(L, "SetHotSpot", &LuaSprite::SetHotSpot);
	ELuna::registerMethod<LuaSprite>(L, "SetFlip", &LuaSprite::SetFlip);
	ELuna::registerMethod<LuaSprite>(L, "SetColor", &LuaSprite::SetColor);
	ELuna::registerMethod<LuaSprite>(L, "GetTexture", &LuaSprite::GetTexture);
	ELuna::registerMethod<LuaSprite>(L, "IsTextureFilter", &LuaSprite::IsTextureFilter);
	ELuna::registerMethod<LuaSprite>(L, "GetColor", &LuaSprite::GetColor);
	ELuna::registerMethod<LuaSprite>(L, "GetZ", &LuaSprite::GetZ);
	ELuna::registerMethod<LuaSprite>(L, "GetBlendMode", &LuaSprite::GetBlendMode);
	ELuna::registerMethod<LuaSprite>(L, "GetWidth", &LuaSprite::GetWidth);
	ELuna::registerMethod<LuaSprite>(L, "GetHeight", &LuaSprite::GetHeight);
	ELuna::registerMethod<LuaSprite>(L, "GetP", &LuaSprite::GetP);
	ELuna::registerMethod<LuaSprite>(L, "SetP", &LuaSprite::SetP);
	ELuna::registerMethod<LuaSprite>(L, "GetRefCount", &LuaSprite::GetRefCount);
	ELuna::registerMethod<LuaSprite>(L, "AddRef", &LuaSprite::AddRef);
	ELuna::registerMethod<LuaSprite>(L, "Release", &LuaSprite::Release);

	ELuna::registerMethod<LuaSprite>(L, "GetTextureRect", &LuaSprite::GetTextureRect);
	ELuna::registerMethod<LuaSprite>(L, "GetHotSpot", &LuaSprite::GetHotSpot);
	ELuna::registerMethod<LuaSprite>(L, "GetFlip", &LuaSprite::GetFlip);
	lua_getglobal(L, gname);
	lua_pushnil(L);
	lua_setglobal(L,gname);
	return 1;
}